//JavaScript Framework 2.0 Code
try {
	Type.registerNamespace('com.yonyou.ncmm_pac_pda.InvCountScanListPageController');
	com.yonyou.ncmm_pac_pda.InvCountScanListPageController = function() {
		com.yonyou.ncmm_pac_pda.InvCountScanListPageController.initializeBase(this);
		this.initialize();
	}

	function com$yonyou$ncmm_pac_pda$InvCountScanListPageController$initialize() {
		//you can programing by $ctx API
		//get the context data through $ctx.get()
		//set the context data through $ctx.push(json)
		//set the field of the context through $ctx.put(fieldName, fieldValue)
		//get the parameter of the context through $ctx.param(parameterName)
		//Demo Code:
		//    var str = $ctx.getString();      //获取当前Context对应的字符串
		//    alert($ctx.getString())          //alert当前Context对应的字符串
		//    var json = $ctx.getJSONObject(); //获取当前Context，返回值为json
		//    json["x"] = "a";        //为当前json增加字段
		//    json["y"] = [];           //为当前json增加数组
		//    $ctx.push(json);            //设置context，并自动调用数据绑定
		//
		//    put方法需手动调用databind()
		//    var x = $ctx.get("x");    //获取x字段值
		//    $ctx.put("x", "b");     //设置x字段值
		//    $ctx.put("x", "b");     //设置x字段值
		//    $ctx.databind();            //调用数据绑定才能将修改的字段绑定到控件上
		//    var p1 = $param.getString("p1");   //获取参数p2的值，返回一个字符串
		//    var p2 = $param.getJSONObject("p2");   //获取参数p3的值，返回一个JSON对象
		//    var p3 = $param.getJSONArray("p3");   //获取参数p1的值，返回一个数组

		//your initialize code below...

	}

	function com$yonyou$ncmm_pac_pda$InvCountScanListPageController$evaljs(js) {
		eval(js)
	}

	var popDialogInfoForScan = {
		"viewid": "panel11", //显示的控件的id，通常是一个容器控件div
		// "animation-direction" : "bottom", //top | bottom | left | right,弹出的方向
		"popgravity": "popbottom", //popcenter|poptop|popbottom|popleft|popright,弹出后最终显示的位置
		// istouch : 'false'
	};

	function com$yonyou$ncmm_pac_pda$InvCountScanListPageController$imagebutton0_onclick(sender, args) {
		// 点击按钮弹出浮动菜单
		$menu.openDropDownList({
			"controlid": "imagebutton0",
			"dropDownListWidth": "100", //菜单项的宽度
			"background": "#f2f2f2",
			//"background-image":"app.png",
			"color": "#ee4311",
			"font-size": "12",
			"halign": "center", //right  center
			"split-color": "#ee4311",
			//"margin-left":"10",
			"panelstyle": "round-div",
			"border-color": "#ee4311",
			"dropItemsArray": [{
				"name": "主界面",
				"action": "menu1_click()"
			}, {
				"name": "数据管理",
				"action": "menu2_click()"
			}, {
				"name": "业务设置",
				"action": "menu3_click()"
			}, {
				"name": "注销",
				"action": "menu4_click()"
			}]
		})
	}

	function menu1_click() {
		// 跳转到主界面
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.HomePage",
			isKeep: "false"
		})
	}

	function menu2_click() {
		// 跳转到数据管理界面
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.DM_main",
			isKeep: "false"
		})
	}

	function menu3_click() {
		// 跳转到业务设置界面
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.BusinessSettingPage",
			isKeep: "false"
		})
	}

	function menu4_click() {
		// 跳转到登录界面
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.LoginPage",
			isKeep: "false"
		})
	}

	function com$yonyou$ncmm_pac_pda$InvCountScanListPageController$onload(sender, args) {
		// onloadControlForScanlist();
		var json = {};
		if($cache.read("isinvcountscancreated") == "true") {
			queryScanDataFromLocalDB();
		}
	}

	function onloadControlForScanlist() {
		var rightflipperForScanlist = [{
			"halign": "center",
			"text-align": "right",
			"width": "70",
			"icon-width": "12",
			"istogglebutton": "false",
			"font-pressed-color": "#ffffff",
			"icon-height": "12",
			"height": "59",
			"color": "#ffffff",
			"layout-type": "linear",
			"background": "#FF615A",
			"font-size": "12",
			"value": "删除",
			"font-family": "default",
			"valign": "center",
			"checked": "false",
			"icon-text-spacing": "8",
			"onclick": "onItemDelete()"
		}, {
			"halign": "center",
			"text-align": "right",
			"width": "70",
			"icon-width": "12",
			"istogglebutton": "false",
			"font-pressed-color": "#ffffff",
			"icon-height": "12",
			"height": "59",
			"color": "#ffffff",
			"layout-type": "linear",
			"background": "#00ff40",
			"font-size": "12",
			"value": "修改",
			"font-family": "default",
			"valign": "center",
			"checked": "false",
			"icon-text-spacing": "8",
			"onclick": "onItemUpdate()"
		}];
		$id("wlistviewdefine0").set("slidingitems", rightflipperForScanlist);
	}

	// 列表项左划删除事件
	function onItemDelete() {
		if($confirm("确认要删除当前行数据么？")) {
			var row = $id("wlistviewdefine0").get("row");
			deleteRow($stringToJSON(row));
		}
	}

	function deleteRow(row) {
		// 从本地数据删除，然后重新查询数据进行加载
		NCMMPubUtil.deleteVO(INVCOUNTSCANVO, row);
		// 更新产成品入库单增加扫描数量
		INVCOUNTUtil.updateScanNumForList(row["pkitem"]);
		queryScanDataFromLocalDB();
	}

	// 列表项左划修改事件
	function onItemUpdate() {
		if($confirm("确认要修改当前行数据么？")) {
			var row = $id("wlistviewdefine0").get("row");
			$view.open({
				viewid: "com.yonyou.ncmm_pac_pda.InvCountScanPage",
				isKeep: "true",
				"row": row
			})

		}
	}
	// 从本地数据库查询数据进行页面加载
	function queryScanDataFromLocalDB() {
		//先按照单据号和行号进行分组
		var groupsql = getGroupQuerySql();
		var groupdata = NCMMPubUtil.queryBySQL(groupsql);
		groupdata = eval(groupdata);
		//处理反斜杠特殊字符
		groupdata = NCMMPubUtil.htmlspecialjsonarray2UI(groupdata);
		fillRowDatas(groupdata);
		//处理精度
		var headnumkeys = new Array(INVCOUNTSCANVO.SCANSUMNUM);
		var numkeys = new Array(INVCOUNTSCANVO.SCANSINGLENUM.name);
		INVCOUNTUtil.processGroupScale(groupdata, headnumkeys, numkeys, INVCOUNTSCANVO.PK_MEASDOC.name);
		var json = {
			source: eval(groupdata)
		};
		$ctx.push(json);
	}
	//获取分组sql，按照单据号+行号
	function getGroupQuerySql() {
		var sql = "SELECT " +
			INVCOUNTSCANVO.VBILLCODE.name + ", " + INVCOUNTSCANVO.CROWNO.name + ", " +
			INVCOUNTSCANVO.CINVCODE.name + ", " + INVCOUNTSCANVO.CINVNAME.name + ", sum(" + INVCOUNTSCANVO.SCANSINGLENUM.name + ") as " + INVCOUNTSCANVO.SCANSUMNUM +
			" FROM " + INVCOUNTSCANVO.prototype.getTableName() +
			" group by " + INVCOUNTSCANVO.VBILLCODE.name + ", " + INVCOUNTSCANVO.CROWNO.name + ", " + INVCOUNTSCANVO.CINVCODE.name + ", " + INVCOUNTSCANVO.CINVNAME.name;

		return sql;
	}
	//获取分组行信息
	function fillRowDatas(groupdata) {
		for(var i = 0; i < groupdata.length; i++) {
			var vbillcode = groupdata[i][INVCOUNTSCANVO.VBILLCODE.name];
			var rowno = groupdata[i][INVCOUNTSCANVO.CROWNO.name];
			var wheresql = "where " + INVCOUNTSCANVO.VBILLCODE.name + "= '" + vbillcode + "' and " + INVCOUNTSCANVO.CROWNO.name + "= '" + rowno + "'";
			NCMMPubUtil.queryVO(INVCOUNTSCANVO, wheresql, function(rowdata) {
				rowdata = eval(rowdata);
				//处理反斜杠特殊字符
				rowdata = NCMMPubUtil.htmlspecialjsonarray2UI(rowdata);
				groupdata[i]["row"] = rowdata;
			});

		}
		return groupdata;
	}

	function com$yonyou$ncmm_pac_pda$InvCountScanListPageController$button2_onclick(sender, args) {
		$ctx.dataCollect();
		var sourceData = $ctx.getJSONArray("source");
		var recordselected = false;
		var selectrecords = [];
		for(var i = 0; i < sourceData.length; i++) {
			if(sourceData[i]["isgroupchecked"] == null || sourceData[i]["isgroupchecked"] == false || sourceData[i]["isgroupchecked"] == "false") {
				continue;
			}
			var rowdatas = sourceData[i]["row"];
			if(rowdatas == null || rowdatas == "" || rowdatas == "[]") {
				continue;
			}
			recordselected = true;
			for(var j = 0; j < rowdatas.length; j++) {
				selectrecords.push(rowdatas[j]);
			}
		}
		if(recordselected == false) {
			$toast("当前没有可提交的数据，请重新选择！");
			return;
		}
		//将扫描记录转换为提交NC的数据
		var billinfos = INVCOUNTUtil.changeVOTOBillInfos(selectrecords);
		// 提交
		INVCOUNTUtil.submitdataToNC(selectrecords, billinfos, refreshSubmitdata);
	}
	//刷新提交界面数据
	function refreshSubmitdata() {
		// 重新加载当前页面
		queryScanDataFromLocalDB();
		$toast("当前所选数据提交成功！")
	}

	function com$yonyou$ncmm_pac_pda$InvCountScanListPageController$checkbox0_onchange(sender, args) {
		$ctx.dataCollect();
		var isChecked = $id(sender).get("checked");
		var sourceData = $ctx.getJSONArray("source");
		for(var i = 0; i < sourceData.length; i++) {
			sourceData[i]["isgroupchecked"] = isChecked;
			for(var j = 0; j < (sourceData[i]["row"]).length; j++) {
				((sourceData[i]["row"])[j])["ischecked"] = isChecked;
			}
		}
		$ctx.put("source", sourceData);
		$ctx.dataBind();

	}

	function com$yonyou$ncmm_pac_pda$InvCountScanListPageController$button0_onclick(sender, args) {
		$view.close();

	}

	function com$yonyou$ncmm_pac_pda$InvCountScanListPageController$checkbox1_onchange(sender, args) {
		// 单选逻辑
		$ctx.dataCollect();
		var ischecked = $id(sender).get("checked");
		var isallchecked = true;
		if(ischecked == false) {
			isallchecked = false;
		} else {
			var orders = $ctx.getJSONArray("list");
			for(var i = 0; i < orders.length; i++) {
				if(orders[i]["ischecked"] == null || orders[i]["ischecked"] == "false") {
					isallchecked = false;
					break;
				}
			}
		}
		var json = {};
		json["list"] = orders;
		json["isallchecked"] = isallchecked;
		$ctx.push(json);
	}

	function com$yonyou$ncmm_pac_pda$InvCountScanListPageController$onitemclick(sender, args) {
		/*// 获取当前行内容
		 var row = $id("wlistviewdefine0").get("row");
		 row = $stringToJSON(row);
		 // 单击行跳转到详情页面
		 $view.open({
		 "viewid" : "com.yonyou.ncmm_pac_pda.InvCountScanDetailPage",
		 "isKeep" : "true",
		 "row" : row
		 });
		 */
		var row = {};
		row = $id("wlistviewdefine0").get("row");
		row = $stringToJSON(row);
		if(row && row["row"])
			return;
		$view.openPop(popDialogInfoForScan);
	}

	function com$yonyou$ncmm_pac_pda$InvCountScanListPageController$selectdata(sender, args) {
		// 按照查询条件进行快速查询
		NCMMPubUtil.quickFilter();

	}

	function com$yonyou$ncmm_pac_pda$InvCountScanListPageController$onclick_Detail(sender, args) {
		// 详情
		$view.closePop(popDialogInfoForScan);
		// 获取当前行内容
		var row = $id("wlistviewdefine0").get("row");
		//row = $stringToJSON(row);
		// 单击行跳转到详情页面
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.InvCountScanDetailPage",
			"isKeep": "true",
			"row": row
		});

	}

	function com$yonyou$ncmm_pac_pda$InvCountScanListPageController$onclick_Modify(sender, args) {
		// 修改
		$view.closePop(popDialogInfoForScan);
		if($confirm("确认要修改当前行数据么？")) {
			var row = $id("wlistviewdefine0").get("row");
			$view.open({
				viewid: "com.yonyou.ncmm_pac_pda.InvCountScanPage",
				isKeep: "true",
				"row": row
			})
		}
	}

	function com$yonyou$ncmm_pac_pda$InvCountScanListPageController$onclick_Delete(sender, args) {
		// 删除
		$view.closePop(popDialogInfoForScan);
		if($confirm("确认删除所选记录吗？")) {
			var row = $id("wlistviewdefine0").get("row");
			deleteRow($stringToJSON(row));
		}
	}

	function com$yonyou$ncmm_pac_pda$InvCountScanListPageController$onclick_Cancel(sender, args) {
		// 取消
		$view.closePop(popDialogInfoForScan);
	}

	function com$yonyou$ncmm_pac_pda$InvCountScanListPageController$onRestartPage(sender, args) {
		var json = {};
		if($cache.read("isinvcountscancreated") == "true") {
			queryScanDataFromLocalDB();
		}
	}

	function ongroupcheckedclick(sender, args) {
		$ctx.dataCollect();
		var isCheckedAll = true;
		var sourceData = $ctx.getJSONArray("source");
		for(var i = 0; i < sourceData.length; i++) {
			if(sourceData[i]["isgroupchecked"] == null || sourceData[i]["isgroupchecked"] == false || sourceData[i]["isgroupchecked"] == "false") {
				isCheckedAll = false;
				for(var j = 0; j < (sourceData[i]["row"]).length; j++) {
					((sourceData[i]["row"])[j])["ischecked"] = false;
				}
			} else {
				for(var j = 0; j < (sourceData[i]["row"]).length; j++) {
					((sourceData[i]["row"])[j])["ischecked"] = true;
				}
			}
		}
		$ctx.put("isAllChecked", isCheckedAll); //更新全选复选框的界面显示
		$ctx.put("source", sourceData);
		$ctx.dataBind();

	}
	com.yonyou.ncmm_pac_pda.InvCountScanListPageController.prototype = {
		onRestartPage: com$yonyou$ncmm_pac_pda$InvCountScanListPageController$onRestartPage,
		onclick_Cancel: com$yonyou$ncmm_pac_pda$InvCountScanListPageController$onclick_Cancel,
		onclick_Delete: com$yonyou$ncmm_pac_pda$InvCountScanListPageController$onclick_Delete,
		onclick_Modify: com$yonyou$ncmm_pac_pda$InvCountScanListPageController$onclick_Modify,
		onclick_Detail: com$yonyou$ncmm_pac_pda$InvCountScanListPageController$onclick_Detail,
		selectdata: com$yonyou$ncmm_pac_pda$InvCountScanListPageController$selectdata,
		onitemclick: com$yonyou$ncmm_pac_pda$InvCountScanListPageController$onitemclick,
		checkbox1_onchange: com$yonyou$ncmm_pac_pda$InvCountScanListPageController$checkbox1_onchange,
		button0_onclick: com$yonyou$ncmm_pac_pda$InvCountScanListPageController$button0_onclick,
		checkbox0_onchange: com$yonyou$ncmm_pac_pda$InvCountScanListPageController$checkbox0_onchange,
		button2_onclick: com$yonyou$ncmm_pac_pda$InvCountScanListPageController$button2_onclick,
		onload: com$yonyou$ncmm_pac_pda$InvCountScanListPageController$onload,
		imagebutton0_onclick: com$yonyou$ncmm_pac_pda$InvCountScanListPageController$imagebutton0_onclick,
		initialize: com$yonyou$ncmm_pac_pda$InvCountScanListPageController$initialize,
		evaljs: com$yonyou$ncmm_pac_pda$InvCountScanListPageController$evaljs
	};
	com.yonyou.ncmm_pac_pda.InvCountScanListPageController.registerClass('com.yonyou.ncmm_pac_pda.InvCountScanListPageController', UMP.UI.Mvc.Controller);
} catch(e) {
	$e(e);
}